package com.xh.one2one.knowledgeSystem.controller;

import com.xh.core.util.StringUtils;
import com.xh.one2one.knowledgeSystem.service.IKnowledgeTemplateModuleService;
import com.xh.one2one.knowledgeSystem.model.KnowledgeTemplateModule;
import com.xh.one2one.knowledgeSystem.bean.request.KnowledgeTemplateModuleRequest;
import com.xh.core.bean.XhExportHeaderBean;
import org.jsoup.Jsoup;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.xh.core.bean.AjaxResult;
import com.xh.core.bean.XhPage;
import com.xh.core.constant.AjaxResultConstant;
import org.springframework.validation.annotation.Validated;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.List;

import com.xh.core.controller.BaseController;

/**
 * <p>
 * 知识元模板的模块 前端控制器
 * </p>
 *
 * @author tw
 * @since 2020-12-22
 */
@RestController
@RequestMapping("/admin/knowledgeTemplateModule")
public class KnowledgeTemplateModuleController extends BaseController {

    @Autowired
    private IKnowledgeTemplateModuleService knowledgeTemplateModuleService;

    /**
     * 分页数据查询
     * @param page 分页对象
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/queryPage")
    public XhPage queryPage(XhPage page, KnowledgeTemplateModuleRequest bean){
        page=knowledgeTemplateModuleService.queryKnowledgeTemplateModulePage(page,bean);
        return page;
    }

    /**
     * 数据查询
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/query")
    public AjaxResult query(KnowledgeTemplateModuleRequest bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",knowledgeTemplateModuleService.queryKnowledgeTemplateModule(bean));
    }

    /**
     * 导出全部
     * @param bean
     * @return
     */
    @PostMapping(value="/exportAll")
    public AjaxResult exportAll(KnowledgeTemplateModuleRequest bean, HttpServletRequest request) {
        try {
            LinkedHashMap<String,XhExportHeaderBean> exportHeaderBeans=new LinkedHashMap<>();
            //exportHeaderBeans.put("username",new XhExportHeaderBean("用户名"));
            //exportHeaderBeans.put("deleted",new XhExportHeaderBean("是否删除",ImmutableMap.of(0,"否",1,"是")));
            //exportHeaderBeans.put("createTime",new XhExportHeaderBean("创建时间","yyyy-MM-dd HH:mm:ss"));

            String downloadUrl=knowledgeTemplateModuleService.exportAll(request,exportHeaderBeans,bean,"queryKnowledgeTemplateModulePage");
            if(downloadUrl!=null){
                return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",downloadUrl);
            }else{
                return new AjaxResult(AjaxResultConstant.Result.ERROR,"导出失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(AjaxResultConstant.Result.ERROR);
        }
    }

    /**
     * 新增和保存
     * @param knowledgeTemplateModule 实体对象
     * @return
     */
    @PostMapping(value="/saveOrUpdate")
    @Transactional
    public AjaxResult saveOrUpdate(@Validated @RequestBody KnowledgeTemplateModule knowledgeTemplateModule){
        if(knowledgeTemplateModule.getId()==null){
            knowledgeTemplateModuleService.sortKnowledgeTemplateModule(knowledgeTemplateModule);
            knowledgeTemplateModuleService.insert(knowledgeTemplateModule);
        }else{
            KnowledgeTemplateModuleRequest knowledgeTemplateModuleRequest = new KnowledgeTemplateModuleRequest();
            BeanUtils.copyProperties(knowledgeTemplateModule,knowledgeTemplateModuleRequest);
            List<KnowledgeTemplateModule> list = knowledgeTemplateModuleService.queryKnowledgeTemplateModule(knowledgeTemplateModuleRequest);
            KnowledgeTemplateModule module = list.get(0);
            //title 为空 ， 格式化出来cssConent的内容，设置成title
            if(StringUtils.isEmpty(knowledgeTemplateModule.getTitle())){
                //去掉html标签,将前台富文本内容取出来
                String cssContent = knowledgeTemplateModule.getCssContent();
                String text = Jsoup.parse(cssContent).text();
                knowledgeTemplateModule.setTitle(text);
            }
            // cssContent富文本为空，使用title的值，替换 cssContent 富文本的内容，保留样式
            if(StringUtils.isEmpty(knowledgeTemplateModule.getCssContent())){
                String title = module.getTitle();
                String cssContent = module.getCssContent();
                String replaceStr = null;
                if(StringUtils.isEmpty(cssContent)){
                    replaceStr = knowledgeTemplateModule.getTitle();
                }else{
                    replaceStr = cssContent.replaceAll(title, knowledgeTemplateModule.getTitle());
                }
                knowledgeTemplateModule.setCssContent(replaceStr);
            }

            knowledgeTemplateModuleService.update(knowledgeTemplateModule);
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @PostMapping("/deleteById/{id}")
    @Transactional
    public AjaxResult deleteById(@PathVariable String id){
        knowledgeTemplateModuleService.delete(id);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 批量删除操作
     * @param ids
     * @return
     */
    @PostMapping("/batchDelete")
    @Transactional
    public AjaxResult batchDelete(String[] ids){
        knowledgeTemplateModuleService.batchDelete(ids);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }
}

